<?php

	/**
	 * Crawls beer item from beerlog.com
	 *
	 * @author anton.perkin
	 * @version 1.0
	 *
	 */
	class BeerDB implements BeerSource
	{
		/* VARIABLES */
		
		/**
		 * Beer list base URL
		 *
		 * @var string
		 */
		public $listUrl = 'http://www.brewerydb.com/beer';
		
		/**
		 * Beer item base URL
		 *
		 * @var string
		 */
		public $itemUrl = 'http://www.brewerydb.com/beer/info/id/*';
		
		/**
		 * Beer URLs
		 *
		 * @var array
		 */
		public $beerURLs;
		
		
		/* CONSTRUCTORS */
			
		/**
		 * Defualt constructor
		 *
		 */
		public function BeerDB()
		{
			
		}		
		
		
		/* METHODS */			
				
		/**
		 * Gets beer URLs list
		 *
		 * @param int $fromRecord offset
		 * @param int $toRecord count
		 */
		public function GetSavedBeerURLs($fromRecord = '', $toRecord = '')
		{
			for ($i = $fromRecord + 1; $i <= $fromRecord + $toRecord; $i++) 
			{
				$urls[$i]['id'] = $i;
				$urls[$i]['url'] = str_replace('*', $i, $this->itemUrl);
			}
			
			return $urls;
		}						
		
		/**
		 * Gets beer item
		 * 
		 * @param int $id beer id
		 * 
		 * @return BeerItem
		 *
		 */
		public function GetBeerItem($url, $beerId)
		{
			//parsed page
			$page = phpQuery::newDocumentFileHTML($url);

			//beer info
			$beerInfo = $page->find("#content");
			
			//beer item
			$beerItem = new BeerItem();
			
			$beerItem->id = $beerId;
			
			//name
			$beerItem->name = trim($beerInfo->find(".hgroup h1")->html());
			
			//details
			$infoColumns = $beerInfo->find(".project-details dd");
			foreach ($infoColumns as $key => $item)
			{
				$items[$key] = trim(pq($item)->html());
			}
			
			$beerItem->category = $items[0];
			$beerItem->style = $items[1];
			$beerItem->available = $items[2];
			$beerItem->abv = $items[3];
			$beerItem->ibu = $items[4];
			$beerItem->color = $items[5];
			$beerItem->brewery = strip_tags($items[7]);
			
			//description
			$beerItem->description = strip_tags($beerInfo->find(".project-content > p")->html());
			
			//item url
			$beerItem->webAddress = $url;
			
			//image url
			$beerItem->photoURL = 'http://www.brewerydb.com'.$beerInfo->find(".project-details > img")->attr('src');
//			$beerItem->photoURL = $beerInfo->find(".slides > li > img")->attr('src');
			
			//source
			$beerItem->source = 'beerdb';

			return $beerItem;
		}
		
		/**
		 * Gets beer image
		 * 
		 * @param int $id beer id
		 * 
		 * @return BeerItem
		 *
		 */
		public function GetBeerItemImage($beerId)
		{
			//parsed page
			$page = phpQuery::newDocumentFileHTML(str_replace('*', $beerId, $this->itemUrl));
			
			//image url
			$image = $page->find(".project-details > img")->attr('src');

			return (!empty($image)) ? 'http://www.brewerydb.com'.$image : '';
		}		
		
		/**
		 * Gets brewery country
		 * 
		 * @param int $id beer id
		 * 
		 * @return string
		 *
		 */
		public function GetBeerItemCountry($beerId)
		{
			//beer page
			$page = phpQuery::newDocumentFileHTML(str_replace('*', $beerId, $this->itemUrl));
			
			//brewery URL
			$beerInfo = $page->find("#content");
			$infoColumns = $beerInfo->find(".project-details dd");
			foreach ($infoColumns as $key => $item)
			{
				$items[$key] = pq($item);
			}
			if (isset($items[7]))
			{
				$breweryURL = 'http://www.brewerydb.com'.$items[7]->find('a')->attr('href');			
	
				//brewery page
				$breweryPage = phpQuery::newDocumentFileHTML($breweryURL);
				
				//brewery country
				$beerInfo = $breweryPage->find("#content");
				$infoColumns = $beerInfo->find(".project-details dd");
				foreach ($infoColumns as $key => $item)
				{
					$items[$key] = trim(pq($item)->html());
				}
				$country = $items[1];	
			}	
			else 
			{
				$country = '';
			}	
			
			return $country;
		}		
		
	}

?>